<?php

class User extends MY_Controller{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
		$this->load->helper('url');
		$this->load->library('session');
		$this->base_url = array('base_url' => $this->config->item('base_url'));
		$this->assign('base_url', $this->base_url);
	}

	public function login()
	{
		if (!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['identity'])){
			$username = $_POST['username'];
			$password = $_POST['password'];
			$identity = $_POST['identity'];
			if($identity === 'diner'){
				$result = $this->user_model->check_diner($username, $password);
				if ($result){
					$did = $result[0]->id;
					$_SESSION['id'] = $did;
					$_SESSION['type'] = 1;
					$_SESSION['order'] = array();
					redirect('diner/index');
				}else{
					echo "<script>alert('用户名或密码错误');</script><script>history.back();</script>";
				}
			}elseif ($identity === 'rider'){
				$result = $this->user_model->check_rider($username, $password);
				if ($result){
					$rid = $result[0]->id;
					$_SESSION['id'] = $rid;
					$_SESSION['type'] = 2;
					redirect('rider/index');
				}else{
					echo "<script>alert('用户名或密码错误');</script><script>history.back();</script>";
				}
			}elseif ($identity === 'merchant'){

			}

		} else {
			$this->display('login/index.tpl');
		}
	}

	public function logout()
	{
		session_destroy();
		redirect('user/login');
	}
}
